var table = null;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'upload', 'util'], function () {
        lui = layui;
        var form = layui.form;
        var upload = layui.upload;
        var util = layui.util;
        $ = layui.$;

        //载入全宗信息
        table = layui.table;
        table.render({
            elem: '#dataTable'
            , url: '/project/getProjectInfo'
            , method: 'get'
            , cellMinWidth: 50
            , page: true
            , limit: 50
            , height: 'full-65'
            , loading: true
            , size: 'sm'
            , cols: [
                [ //标题栏
                    {field: 'status', title: '状态', fixed: 'left', width: 50, templet: "#statusTpl"},
                    {field: 'code', title: '项目编号', width: 150, fixed: 'left'},
                    {field: 'name', title: '项目名称', width: 300, fixed: 'left'},
                    {field: 'contractNo', title: '合同号', width: 250},
                    {field: 'clientCode', title: '客户编号', width: 150},
                    {field: 'clientName', title: '客户名称', width: 300},
                    {field: 'projectLeader', title: '项目主管', width: 70},
                    {field: 'completion', title: '完成度', width: 100},
                    {field: 'psStartDate', title: '预计启动日期', width: 85},
                    {field: 'psEndDate', title: '预计结束日期', width: 85},
                    {field: 'startDate', title: '实际启动日期', width: 85},
                    {field: 'endDate', title: '实际结束日期', width: 85},
                    {field: 'remark', title: '备注', width: 120},
                    {fixed: 'right', title: '操作', width: 110, toolbar: '#operationBar'}
                ]
            ]
        });

        var uploadInst = upload.render({
            elem: '#importFile'//绑定元素
            , url: "../project/importProject"
            , type: "POST"
            , accept: 'file'
            , exts: 'pmd'
            , size: 50
            , before: function (obj) {
                layer.load();
            }
            , done: function (res) {
                if (res.code == 0) {
                    table.reload('dataTable');
                    top.parent.layer.msg(res.message, {icon: 1});
                } else {
                    top.parent.layer.msg(res.message, {icon: 2});
                }
                layer.closeAll('loading');
            }
            , error: function () {
                layer.closeAll('loading');
            }
        });
        //查询接口
        form.on('submit(postData)', function (obj) {
            let data = obj.field;
            table.reload('fondsTable', {
                url: '/project/getProjectInfo'
                , method: 'get'
                , where: data
                , page: {
                    curr: 1
                }

            });
            return false;
        });

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑项目信息',
                        content: './project/setProjectInfo?id=' + data.id,
                        area: ['800px', '390px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Buttun_Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'del':
                    top.layer.confirm('确定要删除当前选择的项目信息吗？ ', function (index) {
                        $.ajax({
                            url: '../project/delProject', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            contentType: "application/json",
                            data: JSON.stringify({id: data.id}),
                            success: function (result) {
                                if (result.code == 0) {
                                    table.reload('dataTable');
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                        return false;
                    });
                    return false;
                case 'start':
                    top.layer.open({
                        type: 2,
                        title: '项目启动',
                        content: './project/setProjectStart?id=' + data.id,
                        area: ['500px', '550px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'finish':
                    top.layer.open({
                        type: 2,
                        title: '项目结束',
                        content: './project/setProjectFinish?id=' + data.id,
                        area: ['500px', '550px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
            }
        });

        util.on({
            importProject: function () {
                let file = $("#importFile");
                let fileName = file.val();
                let exts = "pmd";
                //校验文件格式
                let value = "";
                value = value.length === 0
                    ? ((fileName.match(/[^\/\\]+\..+/g) || []) || '')
                    : value;

                if (value.length === 0) return;
                if (exts && !RegExp('\\w\\.(' + exts + ')$', 'i').test(escape(value))) {
                    layer.msg('选择的文件中包含不支持的格式');
                    return false;
                }
                let formDate = new FormData();
                formDate.append("file", file[0].files[0]);
                $.ajax({
                    url: "../project/importProject",
                    type: "POST",
                    data: formDate,
                    dataType: 'json',
                    contentType: false,       //一定要写
                    processData: false,       //一定要写
                    traditional: true,        //这里设置为true
                    success: function (res) {
                        layer.msg(res.message);
                        if (res.status == 200) {
                            table.reload('#dataTable', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                        }
                    }
                });
                var f = document.getElementById('getFile');
                if (f.outerHTML) {
                    f.outerHTML = f.outerHTML;
                } else {
                    f.value = '';
                }
            }

        });

    });
